package com.zzl.mine.util;


import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.PropertyResourceBundle;

public class PropertiesUtils {

	static Logger logger = Logger.getLogger(PropertiesUtils.class);
	
	public static String getPropertyValue(String propertyFile,String propertyKey){
		Properties properties = new Properties();
		if(null == propertyFile || "".equals(propertyFile) || 
			null == propertyKey || "".equals(propertyKey)){
			return "";
		}
		try {
			InputStream fis = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertyFile);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
			properties.load(bufferedReader);
			return properties.getProperty(propertyKey);
        } catch(Exception e) {
        	logger.error(e.getMessage(),e);
        }
		return "";
	}
	
	public static String getPropertyValue(String propertyFile,String propertyKey,String defaultValue){
		if(null == propertyFile || "".equals(propertyFile) || 
				null == propertyKey || "".equals(propertyKey)){
				return "";
			}
		String v = "";
		try {
			InputStream fis = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertyFile);
			PropertyResourceBundle props = new PropertyResourceBundle(fis);
			v = props.getString(propertyKey);
			if(null == v || "".equals(v)){
				v = defaultValue;
			}
        } catch(Exception e) {
        	v = defaultValue;
        	logger.error(e.getMessage(),e);
        }
		return v;
	}
}
